import os
import requests
from dotenv import load_dotenv

# 全局配置变量
GITLAB_CONFIG = {
    'url': 'http://10.2.9.71:8099',  # GitLab服务器地址
    'token': 'glpat-dCmPvbhy3Nz1iWaMZ8TY',  # GitLab访问令牌
    'api_version': 'v4',  # API版本
    'timeout': 30,  # 请求超时时间（秒）
}

def test_gitlab_connection():
    # 构建API请求头
    headers = {
        'PRIVATE-TOKEN': GITLAB_CONFIG['token']
    }
    
    # 构建API URL
    api_url = f"{GITLAB_CONFIG['url']}/api/{GITLAB_CONFIG['api_version']}/user"
    
    # 测试API连接
    try:
        # 获取当前用户信息作为测试
        response = requests.get(
            api_url, 
            headers=headers,
            timeout=GITLAB_CONFIG['timeout']
        )
        
        if response.status_code == 200:
            user_data = response.json()
            print("连接成功！")
            print(f"当前用户: {user_data.get('name')} ({user_data.get('email')})")
            print(f"用户ID: {user_data.get('id')}")
            print(f"用户名: {user_data.get('username')}")
        else:
            print(f"连接失败！状态码: {response.status_code}")
            print(f"错误信息: {response.text}")
            
    except requests.exceptions.RequestException as e:
        print(f"连接错误: {str(e)}")
    except Exception as e:
        print(f"发生未知错误: {str(e)}")

def print_config_info():
    """打印当前配置信息"""
    print("\n当前配置信息：")
    print(f"GitLab URL: {GITLAB_CONFIG['url']}")
    print(f"API 版本: {GITLAB_CONFIG['api_version']}")
    print(f"超时设置: {GITLAB_CONFIG['timeout']}秒")
    print("Token: [已配置]")
    print("-" * 50)

if __name__ == "__main__":
    print_config_info()
    test_gitlab_connection() 